//
//  logViewController.m
//  AIM
//
//  Created by user10 on 2017/9/28.
//  Copyright © 2017年 user10. All rights reserved.
//

#import "logViewController.h"
#import "LogTableViewCell.h"
#import "DataBase.h"
#import "logModel.h"

@interface logViewController () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *logTableView;
@property (weak, nonatomic) IBOutlet UITextField *fromTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *toTextFiled;
@property (weak, nonatomic) UIDatePicker *datePicker;

@property (strong, nonatomic) NSMutableArray *LogArr;
@end

@implementation logViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _logTableView.delegate = self;
    _logTableView.rowHeight = 104;
    _logTableView.dataSource = self;
    _fromTextFiled.delegate = self;
    _toTextFiled.delegate = self;
    // Do any additional setup after loading the view.
    NSMutableArray *LogArr = [NSMutableArray array];
    LogArr= [[DataBase sharedDataBase]getAllLog];
    
    _LogArr = LogArr;
    
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,44)];
    
    UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    //定义完成按钮
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone  target:self action:@selector(finish:)];
    UIBarButtonItem * cleanButton = [[UIBarButtonItem alloc]initWithTitle:@"clean" style:UIBarButtonItemStyleDone  target:self action:@selector(clean)];
    
    //在toolBar上加上这些按钮
    NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,cleanButton,doneButton,nil];
    [bar setItems:buttonsArray];
    _fromTextFiled.inputAccessoryView = bar;
    _toTextFiled.inputAccessoryView = bar;
    // MARK: - 日期选择器属性及样式
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 10, 200, 220)];
    datePicker.datePickerMode = UIDatePickerModeDate;//模式选择
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文
    datePicker.locale = locale;
    _datePicker = datePicker;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];//设置输出的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    // MARK: - 重点的一句
    _fromTextFiled.inputView = datePicker;
    _toTextFiled.inputView = datePicker;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)clean{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    _fromTextFiled.text = NULL;
    _toTextFiled.text = NULL;
    
}
- (void)finish:(UITextField *)textFild{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSDate *date = _datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString  *string = [[NSString alloc]init];
    string = [dateFormatter stringFromDate:date];
    textField.text = string;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _LogArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //指定cellIdentifier为自定义的cell
    static  NSString *CellIdentifier = @"LogTableViewCell" ;
    //自定义cell类
    LogTableViewCell *cell = [_logTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if  (cell == nil) {
        //通过xib的名称加载自定义的cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"LogTableViewCell"  owner:self options:nil] lastObject];
    }
    logModel *log = _LogArr[indexPath.row];
    
    cell.time.text = log.time;
    cell.opration.text = log.opration;
    cell.type.text = log.type;
    cell.filename.text = log.filename;
    return cell;
    
}
- (IBAction)searchBtn:(id)sender {
    //注意空格
    NSString *startStr = [NSString stringWithFormat:@"%@ 00-00-00",_fromTextFiled.text];
    NSString *endStr = [NSString stringWithFormat:@"%@ 99-99-99",_toTextFiled.text];
    
    _LogArr = [[DataBase sharedDataBase]getResultLogWithStartTime:startStr endTime:endStr];
    [_logTableView reloadData];
    if (_fromTextFiled.text.length == 0  || _toTextFiled.text.length == 0) {
        _LogArr = [[DataBase sharedDataBase]getAllLog];
        [_logTableView reloadData];
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
